package com.epkursant.app.login;

import com.ep.general.ActionSupportCustom;
import com.ep.general.app.Utils;
import com.ep.general.app.login.AbstractLoginAction;
import com.ep.general.kursant.Kursant;
import com.ep.general.kursant.KursantMdt;

public class LoginAction extends AbstractLoginAction<Kursant> implements
		LoginRequred {

	private static final long serialVersionUID = 5423815709746494889L;
	private final String mainPage = "mainPage";

	public String toMainPage() {
		return mainPage;
	}

	@Override
	public String execute() throws Exception {
		String login = getLogin();
		String password = getPassword();

		if (login == null || password == null) {
			return LOGIN;
		}

		Kursant kursant = new KursantMdt().get(login,
				Utils.convertToMd5(password));

		if (kursant != null) {
			setLoggedUser(kursant);

			return ActionSupportCustom.START_PAGE;

		} else {
			addActionError("Niepoprawny login lub hasło");
			return LOGIN;
		}
	}

}
